/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.domain.Sensor;
import me.zhengjie.modules.biz.domain.Vidicon;
import me.zhengjie.modules.biz.repository.LabRepository;
import me.zhengjie.modules.biz.service.dto.CabinetDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.VidiconRepository;
import me.zhengjie.modules.biz.service.VidiconService;
import me.zhengjie.modules.biz.service.dto.VidiconDto;
import me.zhengjie.modules.biz.service.dto.VidiconQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.VidiconMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.io.File;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class VidiconServiceImpl implements VidiconService {

    private final VidiconRepository vidiconRepository;
    private final VidiconMapper vidiconMapper;
    private final LabRepository labRepository;

    @Override
    public Map<String,Object> queryAll(VidiconQueryCriteria criteria, Pageable pageable){
        Page<Vidicon> page = vidiconRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<VidiconDto> dto = page.map(vidiconMapper::toDto);
        List<VidiconDto> resultDto = dto.getContent();
        for (VidiconDto vidiconDto:resultDto) {
            Lab lab = labRepository.findById(vidiconDto.getLabId()).orElseGet(Lab::new);
            vidiconDto.setLabName(lab.getName());
        }

        return PageUtil.toPage(dto);
    }

    @Override
    public List<VidiconDto> queryAll(VidiconQueryCriteria criteria){
        List<Vidicon> vidiconList = vidiconRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        List<VidiconDto> vidiconDtoList = vidiconMapper.toDto(vidiconList);
        for (VidiconDto vidiconDto:vidiconDtoList) {
            Lab lab = labRepository.findById(vidiconDto.getLabId()).orElseGet(Lab::new);
            vidiconDto.setLabName(lab.getName());
        }
        return vidiconDtoList;
    }

    @Override
    @Transactional
    public VidiconDto findById(Integer id) {
        Vidicon vidicon = vidiconRepository.findById(id).orElseGet(Vidicon::new);
        ValidationUtil.isNull(vidicon.getId(),"Vidicon","id",id);
        return vidiconMapper.toDto(vidicon);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VidiconDto create(Vidicon resources) {
        return vidiconMapper.toDto(vidiconRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Vidicon resources) {
        Vidicon vidicon = vidiconRepository.findById(resources.getId()).orElseGet(Vidicon::new);
        ValidationUtil.isNull( vidicon.getId(),"Vidicon","id",resources.getId());
        vidicon.copy(resources);
        vidiconRepository.save(vidicon);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            vidiconRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<VidiconDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VidiconDto vidicon : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", vidicon.getName());
            map.put("楼层", vidicon.getFloor());
            map.put("实验室", vidicon.getLabName());
            map.put("IP地址", vidicon.getIp());
            map.put("通讯端口", vidicon.getPort());
            map.put("通道号", vidicon.getChannel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void downloadPicture(File file, HttpServletRequest request, HttpServletResponse response) {
        FileUtil.downloadFile(request, response, file,  true);
    }

    @Override
    public ArrayList<Map<String, Object>> listFloor(Integer floor) {
        ArrayList<Map<String,Object>> arrayList = new ArrayList<>();
        List<Vidicon> vidiconList = vidiconRepository.findByFloor(floor);
        for (Vidicon vidicon:vidiconList) {
            ArrayList<Object> arrayList1 = new ArrayList<>(List.of(vidicon.getCoordinate().replace("[", "").replace("]", "").split(",")));
            HashMap<String,Object> hashMap = new HashMap<>();
            Map<String,Object> map = new HashMap<>();
            hashMap.put("ip",vidicon.getIp());
            hashMap.put("port",vidicon.getPort());
            hashMap.put("user",vidicon.getUser());
            hashMap.put("password",vidicon.getPassword());
            hashMap.put("id",vidicon.getId());
            arrayList1.add(hashMap);
            map.put("position",arrayList1);
            map.put("isOpen", vidicon.getIsOpen());
            map.put("name", vidicon.getName());
            arrayList.add(map);
        }
        return arrayList;
    }

    @Override
    public List<Map<String, Object>> getCountByFloor() {
        return vidiconRepository.getCountByFloor();
    }
}
